//
//  Kratos_SDKInitPresenter_Kra.m
//  kk_framework
//
//  Created by 7yao.top on 2018/11/23.
//  Copyright © 2018年 7yao.top. All rights reserved.
//

#import "Kratos_SDKInitPresenter_Kra.h"
#import "AFNetworking.h"
#import "Kratos_Options.h"
#import "k_const.h"
#import "XTSecurityUtil.h"

@interface Kratos_SDKInitPresenter_Kra()

@property (nonatomic,copy) void (^zhifureslut)(id result);
@property (nonatomic,copy) void (^dataEyeReuslt)(id result);
@end

static Kratos_SDKInitPresenter_Kra *_instance = nil;

@implementation Kratos_SDKInitPresenter_Kra
- (void)usageLogic
{
    Kratos_Options *option = [Kratos_HttpConfigure sharedHttpConfigure].Kra_option;

    [self setupzhifuPick:option];
    [self setupDataEye:option];
    
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
        
    });
    return _instance;
}

+ (void)Kratos_appleIAPCallBackWithGame_kkk:(NSString *)gameid
                                     Kratos_UID:(NSString *)uid
                                   Kratos_price:(NSString *)price
                                   Kratos_Goods:(NSString *)goods
                                     Kratos_Sid:(NSString *)sid
                                  Kratos_Attach:(NSString *)attch
                              Kratos_AppleMeiTu:(NSString *)appleMeiTu
                               Kratos_onSuccess:(void (^)(id responseObject))sucess
                               Kratos_onFailure:(void(^)(NSError *error))failure

{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    dic[@"gameid"] = gameid;
    dic[@"uid"] = uid;
    dic[@"fee"] = price;
    dic[@"sid"] = sid;
    dic[@"sign"] =    [XTSecurityUtil md5:[Kratos_Tool Kratos_getTimestamp]];
    dic[@"ordersn"] = [NSString stringWithFormat:@"%@-%@",[Kratos_Tool Kratos_currentDateStr],[Kratos_Tool Kratos_getTimestamp]];
    dic[@"paytime"] = [Kratos_Tool Kratos_getTimestamp];
    dic[@"attach"] = attch;
    dic[@"pkg"] = [Kratos_HttpConfigure sharedHttpConfigure].Kra_option.Kratos_pkg;
    
    AFHTTPSessionManager *session =  [AFHTTPSessionManager  manager];
    AFHTTPRequestSerializer * requestSerializer = [AFHTTPRequestSerializer serializer];
    [requestSerializer setValue:@"XMLHttpRequest" forHTTPHeaderField:@"X-Requested-With"];
    [requestSerializer setValue:@"http://game.lzgame.top" forHTTPHeaderField:@"Origin"];
    session.requestSerializer = requestSerializer;
    session.responseSerializer = [AFJSONResponseSerializer serializer];
    
    [session POST:[Kratos_HttpConfigure sharedHttpConfigure].Kra_option.Kratos_appleIPAFundsURL parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        sucess(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
    
    
    
}

+ ( nullable instancetype)shareSDKInitPresenterzhifuResult:(void (^)(id))result dataEyeReuslt:(void (^)(id))dataEye
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
        _instance.zhifureslut = result;
        _instance.dataEyeReuslt = dataEye;
    });
    return _instance;
}
///支付切换
- (void)setupzhifuPick:(Kratos_Options *)option
{
    
    
    NSString *FuStatus = [[NSUserDefaults standardUserDefaults] objectForKey:kUserDefaultFuHuan];
    if ([FuStatus isEqualToString:@"3"]) {
        self.zhifureslut(@{@"status":@"1",@"SDKInit":@"0"});
        return;
    }
    
    AFHTTPSessionManager *session =  [AFHTTPSessionManager  manager];
    AFHTTPRequestSerializer * requestSerializer = [AFHTTPRequestSerializer serializer];
    [requestSerializer setValue:@"XMLHttpRequest" forHTTPHeaderField:@"X-Requested-With"];
    [requestSerializer setValue:@"http://game.lzgame.top" forHTTPHeaderField:@"Origin"];
    session.requestSerializer = requestSerializer;
    session.responseSerializer = [AFJSONResponseSerializer serializer];
    
    
    NSMutableString *sss =  [NSMutableString stringWithString:kNEWURL];
    [sss appendFormat:@"/app/igm?"];
    [sss appendFormat:@"gameid=%@&",option.Kratos_GameID];
    [sss appendFormat:@"pkg=%@",option.Kratos_pkg];
    
    
    
    ///支付切换
    [session GET:sss parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject[@"data"] != nil)
        {
            NSString *str = [responseObject objectForKey:@"data"];
            [[NSUserDefaults standardUserDefaults] setObject:str forKey:kUserDefaultFuHuan];
            [[NSUserDefaults standardUserDefaults] synchronize];
            if ([str isEqualToString:@"1"])
            {
                self.zhifureslut(@{@"status":@"1",@"SDKInit":@"1"});
            }else
            {
                self.zhifureslut(@{@"status":@"1",@"SDKInit":@"0"});
            }
        }
     
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        self.zhifureslut(@{@"status":@"0"});
    }];

}
- (void)setupDataEye:(Kratos_Options *)option
{
    AFHTTPSessionManager *session =  [AFHTTPSessionManager  manager];
    AFHTTPRequestSerializer * requestSerializer = [AFHTTPRequestSerializer serializer];
    [requestSerializer setValue:@"XMLHttpRequest" forHTTPHeaderField:@"X-Requested-With"];
    [requestSerializer setValue:@"http://game.lzgame.top" forHTTPHeaderField:@"Origin"];
    session.requestSerializer = requestSerializer;
    session.responseSerializer = [AFJSONResponseSerializer serializer];
    
    
    NSMutableString *sss =  [NSMutableString stringWithString:kNEWURL];
    [sss appendFormat:@"/app/rtv?"];
    [sss appendFormat:@"gameid=%@&",option.Kratos_GameID];
    [sss appendFormat:@"vendor=dataeye"];
    
    
    ///dataEye激活
    [session GET:sss parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]])
        {
            NSString *str = [responseObject objectForKey:@"data"];
            [[NSUserDefaults standardUserDefaults] setObject:str forKey:kUserDefaultDataEyeKey];
            [[NSUserDefaults standardUserDefaults] synchronize];
            self.dataEyeReuslt(str);
        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        kLog(@"failure%@",error);
    }];
}
@end
